package com.es.servlet;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-20 10:16
 */
@WebServlet(urlPatterns = "/testRequest")
public class TestRequestServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取前端的请求方式
        String method = request.getMethod();
        System.out.println("method:" + method);
        //获取头信息
        Enumeration<String> names = request.getHeaderNames();
        while(names.hasMoreElements()){
            String headName = names.nextElement();
            System.out.println(headName+":{");
            Enumeration<String> values = request.getHeaders(headName);
            while(values.hasMoreElements()){
                System.out.println(values.nextElement());
            }
            System.out.println("}");
        }
        //远程主机
        System.out.println("远程主机："+request.getRemoteAddr());
        System.out.println("远程端口:"+request.getRemotePort());
        //
        System.out.println("请求串"+request.getQueryString());
        //参数
        Enumeration<String> paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()){
            String paramName = paramNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            //Files Objects Collections Arrays Strings
            String valueString = StringUtils.join(values,",");
            System.out.println(paramName+":"+valueString);
           // System.out.println(paramNames.nextElement());

        }
        //知道单值的情况下，直接使用gerParameterValue
        String name = request.getParameter("name");
        System.out.println(name);
       // System.out.println(new String(name.getBytes("ISO-8859-1"),"gbk"));
    }
}
